package vn.vfriends.vbox.storage;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
import vn.vfriends.vbox.model.Storage;
import vn.vfriends.vbox.model.File;
import vn.vfriends.vbox.rest.reader.StorageReader;
import vn.vfriends.vbox.rest.reader.ListFileReader;

/**
 *
 * @author tuancn
 */
@Stateless
public class DropboxService {
    
    public Storage getStorage(String token) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://api.dropbox.com/1/account/info").queryParam("access_token", token);
        
        target.register(StorageReader.class);
        
        return target.request(MediaType.APPLICATION_JSON).get(Storage.class);
    }
    
    public List<File> getFiles(String accessToken, String fileId) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://api.dropbox.com/1/metadata/dropbox")
                .path(fileId)
                .queryParam("access_token", accessToken);
        
        target.register(ListFileReader.class);
        
        return target.request(MediaType.APPLICATION_JSON).get(new GenericType<List<File>>() {});
    }
    
    public File copy(String accessToken, String fromPath, String toPath) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://api.dropbox.com/1/fileops/copy")
                .queryParam("access_token", accessToken);
        
        Form form = new Form();
        form.param("root", "dropbox");
        form.param("from_path", fromPath);
        form.param("to_path", toPath);
        
        FileResponse res = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), FileResponse.class);
        
        File file = new File();
        
        return null;
    }
}

@XmlRootElement
class FileResponse {
    private String size;
    private long bytes;
    private Date modified;
    private String path;
    private String is_dir;
    private String mime_type;

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public long getBytes() {
        return bytes;
    }

    public void setBytes(long bytes) {
        this.bytes = bytes;
    }

    public Date getModified() {
        return modified;
    }

    public void setModified(Date modified) {
        this.modified = modified;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getIs_dir() {
        return is_dir;
    }

    public void setIs_dir(String is_dir) {
        this.is_dir = is_dir;
    }

    public String getMime_type() {
        return mime_type;
    }

    public void setMime_type(String mime_type) {
        this.mime_type = mime_type;
    }
}